package com.yikaiye.android.yikaiye.ui.activity;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.annotation.SuppressLint;
import android.os.CountDownTimer;
import android.view.View;

import com.yikaiye.android.yikaiye.application.MyApplication;
import com.yikaiye.android.yikaiye.base.BaseActivity;
import com.yikaiye.android.yikaiye.base.BaseViewModel;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ActivitySplashBinding;
import com.yikaiye.android.yikaiye.dialog.AgreementDialog;
import com.yikaiye.android.yikaiye.ui.activity.login.LoginActivity;
import com.yikaiye.android.yikaiye.utils.CommonUtil;
import com.yikaiye.android.yikaiye.utils.JumpIntent;
import com.yikaiye.android.yikaiye.utils.sharedpreference.SPUtil;

/**
 * 启动页
 */
public class SplashActivity extends BaseActivity<ActivitySplashBinding, BaseViewModel> {

    private AgreementDialog agreementDialog;

    @Override
    protected BaseViewModel bindModel() {
        return getViewModel(this, BaseViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void initData() {
        initDialog();
        animWay(binding.move);
        new CountDownTimer(3000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
            }

            @Override
            public void onFinish() {
                boolean isFirst = SPUtil.getBoolean(Constant.IS_FIRST_KEY, true);
                if (isFirst) {
                    agreementDialog.showDialog();
                } else {
                    JumpIntent.jump(SplashActivity.this, MyApplication.manager.isLogin()
                            ? MainActivity.class : LoginActivity.class, true);
                }
            }
        }.start();
    }

    private void initDialog() {
        agreementDialog = new AgreementDialog(this) {
            @Override
            public void leftClick() {
                dismissDialog();
                MyApplication.getInstance().exitApp();
            }

            @Override
            public void rightClick() {
                if (!CommonUtil.isCanClick()) return;
                dismissDialog();
                JumpIntent.jump(SplashActivity.this, LoginActivity.class, true);
                SPUtil.put(Constant.IS_FIRST_KEY, false);
            }
        };
    }

    public void animWay(final View view) {
        @SuppressLint("ObjectAnimatorBinding") final ObjectAnimator anim = ObjectAnimator
                .ofFloat(view, "Q", 0.0F, 25.0F)//
                .setDuration(2000);//
        anim.start();
        anim.addUpdateListener(animation -> {
            float cVal = (Float) animation.getAnimatedValue();
            view.setAlpha(cVal);
            view.setScaleX(cVal);
            view.setScaleY(cVal);
        });
        anim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                animWay1(binding.word, binding.pierced);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

    private void animWay1(View view, View view1) {
        PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f, 0f);
        PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f, 2f);
        PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f, 2f);
        ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY, pvhZ).setDuration(1000).start();
        PropertyValuesHolder pvhX1 = PropertyValuesHolder.ofFloat("alpha", 1f, 0f);
        ObjectAnimator.ofPropertyValuesHolder(view1, pvhX1).setDuration(1000).start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        agreementDialog.dismissDialog();
    }
}